package dialog;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import screen.Main;
import file.FileManagement;

@SuppressWarnings("serial")
public class AddPerson extends Dialog implements ActionListener {

	/*Component*/
	JTextField id, name, age, phone[], car;
	JPasswordField pwd, rePwd;
	JComboBox<String> prePhone, size, type;
	JRadioButton membership[];
	ButtonGroup member;
	JLabel checkID, iconLabel;
	JButton enroll;
	ImageIcon icon;
	
	/*tag*/
	String tagLabel[] = { "ID", "PWD", "Name", "Age", "Phone", "Car #", "Size",
			"Type", "Member", "enter the password", "re-enter the password",
			"choose your car number", "choose size of your car",
			"choose type which you want to choose" };
	String tagPhone[] = { "010", "011", "016", "017", "019" };
	String tagSize[] = { "Small", "Middle", "Large" };
	
	public AddPerson(Main main, int height, FileManagement fm) {
		super(main, height, fm);
		// TODO Auto-generated constructor stub

		/* Component */
		id = new JTextField();
		checkID= new JLabel("We will automatically check your id");
		pwd = new JPasswordField();
		rePwd = new JPasswordField();
		name = new JTextField();
		age = new JTextField();
		prePhone = new JComboBox<String>();
		phone = new JTextField[2];
		phone[0] = new JTextField();
		phone[1] = new JTextField();
		car = new JTextField();
		size = new JComboBox<String>();
		type = new JComboBox<String>();
		membership = new JRadioButton[2];
		membership[0] = new JRadioButton("non member");
		membership[1] = new JRadioButton("member");
		member = new ButtonGroup();
		enroll = new JButton("Enroll");
		JLabel label[] = new JLabel[14];
		for(int i=0;i<label.length;i++)	label[i] = new JLabel(tagLabel[i]);
		iconLabel = new JLabel();
		icon = new ImageIcon(".\\Images\\Icon_car1.png");

		id.setBounds(57, 35, 122, 22);
		checkID.setBounds(189,35,200,22);
		checkID.setForeground(Color.gray);
		pwd.setBounds(57, 67, 122, 22);
		rePwd.setBounds(57, 99, 122, 22);
		name.setBounds(57, 131, 82, 22);
		age.setBounds(57, 163, 37, 22);
		prePhone.setBounds(57, 195, 52, 22);
		prePhone.setEditable(true);
		for(int i=0;i<tagPhone.length;i++)	prePhone.addItem(tagPhone[i]);
		phone[0].setBounds(124, 195, 37, 22);
		phone[1].setBounds(176, 195, 37, 22);
		car.setBounds(57, 227, 102, 22);
		size.setBounds(57, 259, 62, 22);
		for(int i=0;i<tagSize.length;i++)	size.addItem(tagSize[i]);
		type.setBounds(57, 291, 67, 22);
		for(int i=1;i<=13;i++)	type.addItem("Type"+i);
		for(int i=0;i<membership.length;i++){
			membership[i].setBounds(57+107*i, 360, 102-20*i, 22);
			membership[i].setForeground(Color.WHITE);
			membership[i].setOpaque(false);
			if(i==0)	membership[i].setSelected(true);
		}
		enroll.setBounds(315, 388, 70, 30);
		for(int i=0;i<2;i++){
			label[i].setBounds(10,35+32*i,50,22);
			label[i].setForeground(Color.WHITE);
		}
		for(int i=2;i<9;i++){
			label[i].setBounds(10,67+32*i,50,22);
			label[i].setForeground(Color.WHITE);
			if(i==8) 	label[i].setBounds(10,360,50,22);
		}
		for(int i=9;i<14;i++)	label[i].setForeground(Color.gray);
		label[9].setBounds(189, 67, 200, 22);
		label[10].setBounds(189, 99, 200, 22);
		label[11].setBounds(169, 227, 200, 22);
		label[12].setBounds(129, 259, 200, 22);
		label[13].setBounds(134, 291, 250, 22);
		iconLabel.setIcon(icon);
		iconLabel.setBounds(57, 322, 122, 32);
		
		add(id);
		add(checkID);
		add(pwd);
		add(rePwd);
		add(name);
		add(age);
		add(prePhone);
		add(phone[0]);
		add(phone[1]);
		add(car);
		add(size);
		add(type);
		add(membership[0]);
		add(membership[1]);
		member.add(membership[0]);
		member.add(membership[1]);
		add(enroll);
		add(iconLabel);
		for(int i=0;i<label.length;i++)	add(label[i]);

		/* Listener */
		id.addActionListener(this);
		pwd.addActionListener(this);
		rePwd.addActionListener(this);
		name.addActionListener(this);
		age.addActionListener(this);
		prePhone.addActionListener(this);
		phone[0].addActionListener(this);
		phone[1].addActionListener(this);
		car.addActionListener(this);
		size.addActionListener(this);
		type.addActionListener(this);
		membership[0].addActionListener(this);
		membership[1].addActionListener(this);
		enroll.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Component c = (Component) e.getSource();
		if (c instanceof JComboBox) {
			if (c == prePhone) {
			}
			else if (c== size){
				
			}
			else if (c== type){
				icon = null;
				icon = new ImageIcon(".\\Images\\Icon_car"+(type.getSelectedIndex()+1)+".png");
				iconLabel.setIcon(icon);
			}
		}
	}

}
